package com.green.common.util.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.green.common.util.StringUtil;

/**
 * 格式化类.
 * 
 */
public class Formatter {

	/**
	 * 返回target被包裹后的字符串.
	 * 
	 * @author lineshow created on 2012-2-6
	 * @param targetObj
	 *            被包裹对象
	 * @param stuffLen
	 *            包裹用的数据长度
	 * @param postion
	 *            指定在此位置上 target将被插入至stuff
	 * @param datatype
	 *            stuff类型
	 * @return
	 */
	public static String immerseString(Object targetObj, int stuffLen,
			int postion, CharType charType) {
		String targetString = ObjectUtils.getDisplayString(targetObj);
		if (stuffLen < 0) {
			stuffLen = Math.abs(stuffLen);
		}
		if (stuffLen == 0) {
			return targetString;
		}
		StringBuilder stuffb = new StringBuilder(getRandomString(stuffLen,
				stuffLen, charType));
		if (postion < 0) {
			int realpostion = postion + stuffLen;
			postion = realpostion > 0 ? realpostion : 0;
		} else {
			postion = postion > stuffLen ? stuffLen : postion;
		}
		stuffb.insert(postion, targetString);
		return stuffb.toString();
	}

	/**
	 * 生成随机字符.
	 * 
	 * @author lineshow created on 2011-12-29
	 * @return
	 */
	public static String getRandomChar(CharType charType) {
		int index = charType.getVlaue();
		if (index < 0) {
			index = (int) Math.round(Math.random() * 2);
		}
		String randChar = "";
		switch (index) {
		case 0:// 大写字符
			randChar = String
					.valueOf((char) Math.round(Math.random() * 25 + 65));
			break;
		case 1:// 小写字符
			randChar = String
					.valueOf((char) Math.round(Math.random() * 25 + 97));
			break;
		default:// 数字
			randChar = String.valueOf(Math.round(Math.random() * 9));
			break;
		}
		return randChar;
	}

	/**
	 * 获取指定位数随机字符串.
	 * 
	 * @author lineshow created on 2012-2-6
	 * @param minLen
	 * @param maxLen
	 * @return
	 */
	public static String getRandomString(int minLen, int maxLen) {
		return getRandomString(minLen, maxLen, CharType.MIXED_CHAR);
	}

	/**
	 * @author lineshow created on 2011-12-29
	 * @param minLen
	 *            字符串最短位数
	 * @param maxLen
	 *            字符串最长位数
	 * @param datatype
	 *            [-1:mixed char/0:char-uppercase/1:char-lowercase/2:number]
	 * @return
	 */
	public static String getRandomString(int minLen, int maxLen,
			CharType charType) {
		if (minLen < 0 || maxLen < 0) {
			return "";
		}
		int randomLen = 0;
		if (minLen == maxLen) {
			randomLen = maxLen;
		} else {
			if (minLen > maxLen) {
				minLen = minLen ^ maxLen;
				maxLen = minLen ^ maxLen;
				minLen = minLen ^ maxLen;
			}
			randomLen = (int) (Math.random() * (maxLen + 1 - minLen) + minLen);
		}
		StringBuilder randomContainer = new StringBuilder();
		for (int i = 0; i < randomLen; i++) {
			randomContainer.append(getRandomChar(charType));
		}
		return randomContainer.toString();
	}

	/**
	 * 
	 * @author lineshow created on 2011-10-20
	 * @param material
	 * @param frontCoverLen
	 * @param afterCoverLen
	 * @return
	 */
	public static String submerge(String material, int frontCoverLen,
			int afterCoverLen) {
		return null;
	}

	public static boolean matchEmail(String email) {
		Pattern pattern = Pattern.compile("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+");
		Matcher matcher = pattern.matcher(email);
		return matcher.matches();
	}

	public enum CharType {
		MIXED_CHAR(-1), UPPERCASE_CHAR(0), LOWERCASE_CHAR(1), NUMBERTYPE_CHAR(2);
		private int datatype;

		CharType(int datatype) {
			this.datatype = datatype;
		}

		public int getVlaue() {
			return this.datatype;
		}

		public static CharType getCharType(int datatype) {
			switch (datatype) {
			case 0:
				return CharType.UPPERCASE_CHAR;
			case 1:
				return CharType.LOWERCASE_CHAR;
			case 2:
				return CharType.NUMBERTYPE_CHAR;
			default:
				return CharType.MIXED_CHAR;
			}
		}
	}

	/**
	 * 金额 格式化
	 * 
	 * @param value
	 *            金额
	 * @param format
	 *            格式化 如 ###,###.## ###,###.00 0.00 000,000.00
	 * @return
	 */
	public static String moneyFormat(String value, String format) {
		if (StringUtil.isEmpty(format))
			return value;
		if (StringUtil.isEmpty(value))
			return null;
		try {
			NumberFormat formater = new DecimalFormat(format);
			double num = Double.parseDouble(value);
			return formater.format(num);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 日期 格式化
	 * 
	 * @param date
	 * @param formart
	 * @return
	 */
	public static String dateFormat(Date date, String formart) {
		if (null == date)
			return null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(formart);
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * test access
	 * 
	 * @author lineshow created on 2011-10-20
	 * @param strings
	 */
	public static void main(String... strings) {
		System.out.println(immerseString(312312L, 11, 11,
				CharType.NUMBERTYPE_CHAR));

	}

}
